<?php

namespace App\Http\Controllers\Web\Member\Buyer;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ItemsInformationController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'buyer/items'],
        'show' => ['get', 'buyer/items/%d'],
        'store' => ['post', 'buyer/items'],
        'item_shop' => ['get', 'buyer/items/item_shop'],
        'seller_shop' => ['get', 'buyer/items/seller_shop/%d'],
        'payment' => ['get', 'buyer/payment'],
        'get_payment' => ['post', 'buyer/items/get_payment'],
        'item_order' => ['post', 'buyer/items/item_order'],
        'address_index' => ['get', 'buyer/address'],
        'invoices_index' => ['get', 'buyer/invoices'],
        'item_store' => ['post', 'buyer/items/item_store'],

    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        //参数部分
        $search_params = $this->request->all(['order_code', 'order_status']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'orderBy'=>'id',
            'sortedBy'=>'desc',
        ];

        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);
        //结果处理部分
        $data = paginate($this->request, $ret['data']);

        foreach($data['list'] as $k=>$v){
            $data['list'][$k]['items_information']['quote_contain']=\GuzzleHttp\json_decode($v['items_information']['quote_contain']);
        }
        //回填搜索框值
        $data['search_params'] = $search_params;
        return view('web.user.buyer.match_order', $data);
    }

    public function show($id)
    {
        $ret = http_web($this->api_url['show'],[$id])->send();
        return view('web.user.buyer.matching_eidt', $ret['data']);

    }

    public function create(){

        $ret = http_web($this->api_url['seller_shop'],[session('user_id')])->send();
        return view('web.user.seller.matching_add',$ret['data'][0]);
    }

    public function store(){

        $this->validate($this->request, [
            'buyer_phone' => 'required',
            'buyer_contact' => 'required',
            'buyer_company' => 'required',
            'quote_contain' => 'required',
            'seller_phone' => 'required',
            'seller_contact' => 'required',
            'seller_company' => 'required',
        ]);
        $data=$this->request->all();
        $ret = http_web($this->api_url['store'])->send($data);

        if(isset($ret['code']) && $ret['code']===0 ){
           return response()->json(['code'=>0,'message'=>'',$ret]);
        }else{
           return response()->json(['code'=>1,'message'=>$ret['message']]);
        }

    }

    public function edit($id)
    {
        $ret = http_web($this->api_url['show'],[$id])->send();

        return view('form', ['type' => 'update']);
    }

    public function update($id)
    {
        $this->validate($this->request, [
            'buyer_phone' => 'required',
            'buyer_contact' => 'required',
            'buyer_company' => 'required',
            'buyer_id' => 'required',
            'quote_contain' => 'required',
            'quote_trade_time' => 'required',
            'project_addr' => 'required',
            'seller_phone' => 'required',
            'seller_contact' => 'required',
            'seller_company' => 'required',
            'seller_id' => 'required',
            'goods_total' => 'required',
            'other_total' => 'required',
        ]);
        $data = $this->request->all([]);

        $ret = http_web($this->api_url['update'],[$id])->send($data);

        if(isset($ret['code']) && $ret['code']===0 ){
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }

    }

    public function destroy()
    {

    }

    /**
     * 配单获取用户公司名和姓名
     */
    public function item_shop(){
        $data=$this->request->all();
        $ret = http_web($this->api_url['item_shop'])->send($data);
        if($ret && isset($ret['data'][0])){
            if(empty($ret['data'][0]['company'])){
                $ret['data'][0]['buyer_company']='';
            }else{
                $ret['data'][0]['buyer_company']=$ret['data'][0]['company']['company_name'];
            }
            return response()->json(['code'=>0,'message'=>'','data'=>$ret['data'][0]]);
        }else{
            return response()->json(['code'=>1,'message'=>'手机号匹配失败']);
        }

    }

    /**
     * 支付
     */
    public function payment(){
        $code = $this->request->code;
        if (!empty($code)){
            $payment = http_web($this->api_url['get_payment'])->send(['item_code'=>$code]);
            if (isset($payment['code']) && $payment['code']==0 && isset($payment['data']['all_total'])) {
                $param = [
                    'pay_id'=>0,
                    'total_order_money'=>$payment['data']['all_total']
                ];
                $order_codes = isset($code) ? $code : '';
                $total_order_money = isset($param['total_order_money']) ? $param['total_order_money'] : 0;
                $new_data = [
                    'orderCodes' => $order_codes,
                    'totalAmount' => number_format($total_order_money, 2, ".", ""),
                    'payChannel' => 0,
                    'payAmount' => number_format($total_order_money, 2, ".", ""),
                ];

                $url = '';
                switch ($param['pay_id']) {
                    case '0':
                        $url = config('url.czbank') . 'smartPay?' . http_build_query($new_data);
                        break;

                    default:
                        break;
                }
                Header("Location: $url");
                die;
            }
        }
    }

    public function order_create($id) {

        $data['id']=$id;
        $skus = http_web($this->api_url['item_order'])->send($data);
//        //请求部分-地址
        $address = http_web($this->api_url['address_index'])->send(['type' => 'all']);
//        //请求部分-发票
        $invoices = http_web($this->api_url['invoices_index'])->send(['type' => 'all']);

        $default_invoice = null;
        foreach ($invoices['data'] as $invoice) {
            if ($invoice['is_default'] == 1) {
                $default_invoice = $invoice;
            }
        }
        $ret = [
            'shop' => $skus['data'],
            'address' => $address['data'],
            'default_invoice' => $default_invoice
        ];
        return view('web.order.submit_item', $ret);

    }


    public function order_store(){
        $data=$this->request->all();
        $order = http_web($this->api_url['item_store'])->send($data);
        if($order['code']==0){
            return redirect('/member/buyer/items_pay/'.$order['data']['order_code']);
        }else{
            return redirect()->back();
        }
    }
}